package gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import gulimall.ware.vo.MergeVo;
import gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.web.bind.annotation.*;

import gulimall.ware.entity.PurchaseEntity;
import gulimall.ware.service.PurchaseService;
import gulimall.utils.PageUtils;
import gulimall.utils.R;

import javax.annotation.Resource;


/**
 * 采购信息
 *
 * @author HLF
 * @email 1132814637@qq.com
 * @date 2022-05-11 16:14:49
 */
@RestController
@RequestMapping("/ware/purchase")
public class PurchaseController {
    @Resource
    private PurchaseService purchaseService;


    /**
     * 查询未领取的采购单
     * /ware/purchase/unreceive/list
     */
    @GetMapping("/unreceive/list")
    public R listUnReceive(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryUnReceivePage(params);

        return R.ok().put("page", page);
    }

    /**
     * 合并采购需求
     * /ware/purchase/merge
     * 请求的参数：
     * {
     * purchaseId: 1, //整单id
     * items:[1,2,3,4] //合并项集合
     * }
     */
    @PostMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo) {
        purchaseService.saveMerge(mergeVo);

        return R.ok();
    }

    /**
     * 领取采购单(改变采购单的状态为已领取，改变采购项<采购需求>的状态为正在采购)
     * 请求参数：采购单id集合
     */
    @PostMapping("/received")
    public R received(@RequestBody List<Long> ids) {
        purchaseService.updateStatusByPurchaseIds(ids);

        return R.ok();
    }

    /**
     * 完成采购
     * /ware/purchase/done
     * 请求参数：
     * {
     * id: 123,//采购单id
     * items: [{itemId:1,status:4,reason:""}]//每一个采购项 完成/失败 的详情
     * }
     */
    @PostMapping("/done")
    public R purchaseDone(@RequestBody PurchaseDoneVo purchaseDoneVo) {
        purchaseService.donePurchase(purchaseDoneVo);

        return R.ok();
    }

    /**
     * 查询采购单列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
